package com.loveto.service;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.ConnectivityManager;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;

import com.loveto.common.CommonUtils;

public class BaseActivity extends Activity{
	
	private Context contextBase;
	private Class<?> moveToClass;
	
	public boolean isNetworkAvailableAndNotice() {
		ConnectivityManager connect = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
		if (CommonUtils.isNetworkAvailabe(connect)) {
			return true;
		} else {
			String msg = "인터넷 연결을 확인하세요.";
			Log.e("tag", msg);
			showSimpleAlert(msg);
			return false;
		}
	}
	
	public boolean isNetworkAvailable() {
		ConnectivityManager connect = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
		if (CommonUtils.isNetworkAvailabe(connect)) {
			return true;
		} else {
			return false;
		}
	}
	
	public void viewToast(String message) {
		viewToast(Gravity.CENTER,0, 0, message);
	}
	
	public void viewToast(int gravity, int xOffset, int yOffset, String message) {
		Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
		toast.setGravity(gravity, xOffset, yOffset);
		toast.show();
	}
	
	public void showSimpleAlert(String msg) {
	   	new AlertDialog.Builder(this)
	   	.setMessage(msg)
	   	.setPositiveButton("확인", new DialogInterface.OnClickListener()
	   	{
	   		@Override
	   		public void onClick(DialogInterface dialog, int which)
	   		{
	   			dialog.dismiss();
	   		}
	   	}).show();
   }
	
	public void showSimpleAlertAndFinish(String msg) {
		new AlertDialog.Builder(this)
		.setMessage(msg)
		.setPositiveButton("확인", new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				dialog.dismiss();
				finish();
			}
		}).show();
	}
	
	public void showSimpleAlertAndMoveActive(String msg,Context getContext,Class<?> moveClass) {
		
		contextBase = getContext;
		moveToClass = moveClass;
		
		new AlertDialog.Builder(this)
		.setMessage(msg)
		.setPositiveButton("확인", new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				dialog.dismiss();
	   			Intent intent;
	   			intent = new Intent(contextBase,moveToClass);
	   			startActivity(intent);
			}
		}).show();
	}
	
	public void showSimpleAlertAndMoveChoice(String msg,Context getContext,Class<?> moveClass,String positiveButtonName) {
		
		contextBase = getContext;
		moveToClass = moveClass;
		
		new AlertDialog.Builder(this)
		.setMessage(msg)
		.setPositiveButton(positiveButtonName, new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				dialog.dismiss();
				Intent intent;
				intent = new Intent(contextBase,moveToClass);
				startActivity(intent);
			}
		})
		.setNegativeButton("취소", new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				dialog.dismiss();
			}
		}).show();
		
	}
	
	public void showSimpleAlert(String title, String msg) {
	   	new AlertDialog.Builder(this)
	   	.setTitle(title)
	   	.setMessage(msg)
	   	.setPositiveButton("확인", new DialogInterface.OnClickListener()
	   	{
	   		@Override
	   		public void onClick(DialogInterface dialog, int which)
	   		{
	   			dialog.dismiss();
	   		}
	   	}).show();
   }
	
	public void showAlertSuspendID(Context getContext,String suspendDate,String suspendReason) {
		//이용정지에 대한 설명 보여주기
		StringBuffer sb = new StringBuffer();
		sb.append(suspendDate).append(" 까지 이용정지상태입니다. 정지기간 동안 정상적인 서비스 이용을 할 수 없습니다.");
		sb.append(" [사유 : ").append(suspendReason).append("]");
		showSimpleAlert(sb.toString());
		
		contextBase = getContext;
		
	   	new AlertDialog.Builder(this)
	   	.setMessage(sb.toString())
	   	.setPositiveButton("확인", new DialogInterface.OnClickListener()
	   	{
	   		@Override
	   		public void onClick(DialogInterface dialog, int which)
	   		{
	   			dialog.dismiss();
	   			//로그아웃 처리
	   			SharedPreferences pref = getSharedPreferences("userInfo", Activity.MODE_PRIVATE);
	   			Editor editor = pref.edit();
	   			editor.clear();
	   			editor.commit();
	   			
	   			//로그인 페이지로 이동
	   			Intent intent;
	   			intent = new Intent(contextBase,LovotoMainActivity.class);
	   			startActivity(intent);
	   		}
	   	}).show();
		
	}
	
	public void showAlertIncorrectPhoneID(Context getContext) {
		contextBase = getContext;
	   	new AlertDialog.Builder(this)
	   	.setMessage("로그인정보가 올바르지 않습니다. 다시 로그인해주세요.")
	   	.setPositiveButton("확인", new DialogInterface.OnClickListener()
	   	{
	   		@Override
	   		public void onClick(DialogInterface dialog, int which)
	   		{
	   			dialog.dismiss();
	   			//로그아웃 처리
	   			SharedPreferences pref = getSharedPreferences("userInfo", Activity.MODE_PRIVATE);
	   			Editor editor = pref.edit();
	   			editor.clear();
	   			editor.commit();
	   			
	   			//로그인 페이지로 이동
	   			Intent intent;
	   			intent = new Intent(contextBase,LoginMainActivity.class);
	   			startActivity(intent);
	   		}
	   	}).show();
		
	}
	
	//화면 전환 시켜주기
	private boolean checkLogin() {
		SharedPreferences pref = getSharedPreferences("userInfo", Activity.MODE_PRIVATE);
		String memberid = pref.getString("memberid", "");
		if(memberid.length() == 0 ) {
			return false;
		} else {
			return true;
		}
	}
	
}
